#include <kore/kore.h>
#include <kore/http.h>
#include "head/log.h"
#include "head/hissl_log.h"

int
hissl_log(struct http_request *req)
{
    int line_byte = sizeof(atklog_t);
    char time_str[TIME_BYTE], sip[SIP_BYTE], dip[DIP_BYTE], severity[SEVERITY_BYTE];
    unsigned int s_ip, d_ip;
    struct tm tm;
    time_t t;

    size_t nread, i;

    struct kore_buf	*buf;

    FILE *fp = fopen(FILE_PATH, "rb");
    fseek(fp, 0, SEEK_END);
    int file_size = ftell(fp);
    int total = file_size / line_byte;//总条数

    http_populate_get(req);

    int	per_page = PER_PAGE;
    if (http_argument_get_uint16(req, "per_page", &per_page)){
        kore_log(LOG_NOTICE, "per_page: %d", per_page);
    }else{
        kore_log(LOG_NOTICE, "No per_page set");
    }

    int c_page_total_byte = line_byte * per_page;
    int c_page_total_byte_tmp = c_page_total_byte;
    double last_page = ceil((float)file_size / (float)c_page_total_byte);//最后一页

    int	current_page = 1;
    if (http_argument_get_uint16(req, "current_page", &current_page)){
        kore_log(LOG_NOTICE, "current_page: %d", current_page);
        if(current_page <= 0){
            current_page = 1;
        }
    }else{
        kore_log(LOG_NOTICE, "No current_page set");
    }

    int file_offset = (current_page -1) * c_page_total_byte;
    int file_offset_tmp = file_size - file_offset;
    if (file_offset_tmp < c_page_total_byte_tmp) {
        if(file_offset_tmp < 0){
            c_page_total_byte = 32;
        } else {
            c_page_total_byte = file_offset_tmp;
        }
    }

    atklog_t *buffer;
    buffer = malloc(c_page_total_byte);
    kore_log(LOG_NOTICE, "c_page_total_byte: %d", c_page_total_byte);

    fseek(fp, file_offset, SEEK_SET);
    nread = fread(buffer, sizeof(atklog_t), per_page, fp);
    fclose(fp);

    buf = kore_buf_alloc(c_page_total_byte);

    //大于最后一页
    if(current_page > (int)last_page){
        kore_log(LOG_NOTICE, "last_page: %d", (int)last_page);
        return response_empty(req, buf, current_page, last_page, per_page, total);
    }

    kore_buf_appendf(buf, "{");
    list_response_public(buf, current_page, last_page, per_page, total);
    kore_buf_appendf(buf, "'data':[");
    for (i = 0; i < nread; i++) {
        atklog_t *s = &buffer[i];
        t = s->time;
        localtime_r(&t, &tm);
        strftime(time_str, sizeof(time_str)-1, "%Y-%m-%d %H:%M:%S", &tm);
        s_ip = s->sip;
        sprintf(sip, "%d.%d.%d.%d", s_ip & 0xFF,(s_ip >> 8) & 0xFF,
                (s_ip >> 24) & 0xFF,	(s_ip >> 16) & 0xFF);
        d_ip = s->dip;
        sprintf(dip, "%d.%d.%d.%d", d_ip & 0xFF,(d_ip >> 8) & 0xFF,
                (d_ip >> 24) & 0xFF,	(d_ip >> 16) & 0xFF);
        if(s->severity == 1){
            sprintf(severity,"高危");
        }else{
            sprintf(severity,"中");
        }

        kore_buf_appendf(buf, "{");
        kore_buf_appendf(buf, "'time':'%s',", time_str);
        kore_buf_appendf(buf, "'sip':'%s',", sip);
        kore_buf_appendf(buf, "'dip':'%s',", dip);
        kore_buf_appendf(buf, "'severity':'%d',", s->severity);
        kore_buf_appendf(buf, "'sport':'%d',", s->sport);
        kore_buf_appendf(buf, "'detail':'%d',", s->detail);
        kore_buf_appendf(buf, "'len':'%d',", s->len);
        kore_buf_appendf(buf, "'ruleid':'%d',", s->ruleid);
        kore_buf_appendf(buf, "'uid':'%d',", s->uid);
        kore_buf_appendf(buf, "'type':'%d',", s->type);
        kore_buf_appendf(buf, "},");
    }
    kore_buf_appendf(buf, "]");
    kore_buf_appendf(buf, "}");

    http_response_header(req, "content-type", "text/json");
    http_response(req, 200, buf->data, buf->offset);
    free(buffer);
    return (KORE_RESULT_OK);
}

int
response_empty(struct http_request *req, struct kore_buf *buf, int current_page, double last_page, int per_page, int total)
{
    kore_buf_appendf(buf, "{");
    list_response_public(buf, current_page, last_page, per_page, total);
    kore_buf_appendf(buf, "'data':[]");
    kore_buf_appendf(buf, "}");

    http_response_header(req, "content-type", "text/json");
    http_response(req, 200, buf->data, buf->offset);
    return (KORE_RESULT_OK);
}

void
list_response_public(struct kore_buf *buf, int current_page, double last_page, int per_page, int total)
{
    kore_buf_appendf(buf, "'current_page':'%d',", current_page);
    kore_buf_appendf(buf, "'last_page':'%.f',", last_page);
    kore_buf_appendf(buf, "'per_page':'%d',", per_page);
    kore_buf_appendf(buf, "'total':'%d',", total);
}
